/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Mobilottery.UI.Forms;

import Mobilottery.Interfaces.IAlertCommandListener;
import Mobilottery.AppManager;
import Mobilottery.Constants.Consts;
import Mobilottery.Constants.Consts.Screens;
import Mobilottery.XmlManager.*;
import javax.microedition.lcdui.*;

/**
 *
 * @author WindowsVista
 */
public class FormMyGames extends List implements CommandListener
{

    MyGamesDetails _myGamesDetails;

    public FormMyGames(Object p_obj)
    {
        super("My Games", IMPLICIT);

        int selectedItem = -1;
        if (p_obj instanceof MyGamesDetails)
        {
            _myGamesDetails = (MyGamesDetails) p_obj;
        }
        else if (p_obj instanceof Object[])
        {
            Object[] arrObj = (Object[]) p_obj;
            selectedItem = ((Integer) arrObj[0]).intValue();
            _myGamesDetails = (MyGamesDetails) arrObj[1];
        }

        super.setFitPolicy(TEXT_WRAP_ON);

        String text;
        Image img;

        for (int i = 0; i < _myGamesDetails.GamesNames.length; i++)
        {
            if (_myGamesDetails.GamesUserWinningAmounts[i] == -1)
            {
                // waiting for results...
                img = Consts.UIConsts.IMAGES_GAMESTATUS_WAITING_FOR_RESULT;

                text = _myGamesDetails.GamesNames[i] + " (waiting...)";
            }
            else if (_myGamesDetails.GamesUserWinningAmounts[i] > 0)
            {
                // won!!!!
                img = Consts.UIConsts.IMAGES_GAMESTATUS_ENDED_WON;

                text = _myGamesDetails.GamesNames[i] + " (" + _myGamesDetails.GamesUserWinningAmounts[i] + "$)";
            }
            else
            {
                // Loss!!!!
                img = Consts.UIConsts.IMAGES_GAMESTATUS_ENDED_LOSS;

                text = _myGamesDetails.GamesNames[i] + " (ended!)";
            }

            super.append(text, img);
        }

        if (selectedItem != -1)
        {
            // set selected index
            setSelectedIndex(selectedItem, true);
        }

        Command cmd;
        cmd = new Command("Select", Command.OK, 0);
        this.setSelectCommand(cmd);
        this.addCommand(cmd);

        cmd = new Command("Back", Command.BACK, 0);
        this.addCommand(cmd);
        this.setCommandListener(this);
    }

    public void commandAction(Command c, Displayable d)
    {
        int cmdType = c.getCommandType();
        if (cmdType == Command.OK)
        {
            menu_Clicked();
        }
        else if (cmdType == Command.BACK)
        {
            back_Clicked();
        }
    }

    private void back_Clicked()
    {
        AppManager.goToScreen(Screens.MainMenu, null);
    }

    private void menu_Clicked()
    {
        int selectedIdx = super.getSelectedIndex();

        AppManager.goToScreen(Screens.MyGameInfo, new Object[]
                {
                    new Integer(selectedIdx),
                    _myGamesDetails
                });
    }
}
